<?php


namespace seecms\lib;

use seecms\lib\builder\control\Control;
use seecms\lib\builder\query\BetweenQuery;
use seecms\lib\builder\query\TextQuery;
use seecms\SeeConst;
use seecms\SeeLang;

class Transfer
{

    /**
     * @param $data
     * @return string
     */
    public static function base64Encode($data): string
    {
        if (empty($data)) {
            return '';
        }

        $data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT);

        return base64_encode($data);
    }

    /**
     * @param $filesize
     * @return string
     */
    public static function filesizeFormatter($filesize): string
    {
        $filesize = intval($filesize);
        if ($filesize <= 0) {
            return '';
        }

        if ($filesize < 1024) {
            return $filesize . 'byte';
        }

        if ($filesize > 1024 * 1024) {
            return sprintf('%.2fmb', $filesize / (1024 * 1024));
        }

        return sprintf('%.2fkb', $filesize / 1024);
    }

    /**
     * @param $query
     * @param $params
     * @return array
     */
    public static function pageQuery($query, $params): array
    {
        $where = [];
        if ($query) {

            foreach ($query as $item) {
                if (!$item instanceof Control) {
                    $item = TextQuery::init($item);
                }

                $name = trim($item->getName());
                $express = $item->getExpress();

                if ($item instanceof BetweenQuery) {
                    $min = $params[$name . '_min'] ?? '';
                    $max = $params[$name . '_max'] ?? '';
                    if ($min > $max) {
                        list($min, $max) = [$max, $min];
                    }

                    if ($min === '') {
                        $min = 0;
                    }

                    if ($max === '') {
                        $max = 0;
                    }

                    $value = [$min, $max];
                } else {
                    $value = $params[$name] ?? null;
                    if (is_string($value)) {
                        $value = trim($value);
                    }
                }

                if ($value !== null && $value !== '') {
                    if ($express === 'date') {
                        $express = 'between';
                        if (strpos($value, '~') !== false) {
                            // 区间
                            $range = explode('~', $value);
                            $start = trim($range[0] ?? '');
                            $end = trim($range[1] ?? '');
                            $end = $end ?: $start;
                            if ($end <= 10) {
                                $end = date('Y-m-d', strtotime($end . ' +1 day'));
                            }
                            $value = [$start, $end];
                        } else {
                            $value = [$value, date('Y-m-d', strtotime($value . ' +1 day'))];
                        }
                    } elseif ($express === 'like') {
                        $value = "%$value%";
                    } elseif ($express == 'in') {
                        $value = explode(',', $value);
                    }
                    $where[$name] = [$express, $value];
                }
            }
        }

        return $where;
    }

    /**
     * @param string|int|null $value
     * @return string|array
     */
    public static function translateVerifyStatus($value = null)
    {
        $data = [
            SeeConst::STATUS_PADDING => SeeLang::rule('verify_padding'),
            SeeConst::STATUS_PASS => SeeLang::rule('verify_pass'),
            SeeConst::STATUS_REFUSE => SeeLang::rule('verify_refuse')
        ];

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }

    /**
     * @param string|int|null $value
     * @return string|array
     */
    public static function translateStatus($value = null)
    {
        $data = [
            SeeConst::STATUS_FORBID => SeeLang::switch('forbid'),
            SeeConst::STATUS_OPEN => SeeLang::switch('open'),
        ];

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }

    /**
     * @param string|int|null $value
     * @return string|array
     */
    public static function translateMenuType($value = null)
    {
        $data = [
            SeeConst::MENU_TYPE_DIRECTORY => '目录',
            SeeConst::MENU_TYPE_MENU => '菜单'
        ];

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }

    public static function translateMenuOpenType($value = null)
    {
        $data = [
            SeeConst::MENU_OPEN_TYPE_IFRAME => 'iframe',
            SeeConst::MENU_OPEN_TYPE_BLANK => 'blank'
        ];

        if ($value !== null) {
            return $data[$value] ?? '';
        }

        return $data;
    }
}